using UnityEngine;
using System.Collections;

public class PlayerActions : MonoBehaviour
{
	private PlayerMovement player;
	
	//maximum distance to stay targeted on an object
	const float MAX_PICKUP_DIST = 115;
	
	
	//how much time can the player shift in his whole life?
	const float LIFETIME_SHIFT_TIME = 100;
	
	//The NPC object
	public NPC npc;
	
	//true if an npc is currently selected for interaction
	private bool npcSelected;
	
	//The NPC the player can currently interact with, if any
	private NPC npcTarget;
	
	//the item targeted
	private GameObject targetItem;
	
	private GameObject heldItem;
	
	//the held item's audio sprite
	AudioSprite heldAudioSprite;
	
	//the y mouse coord where the player begins pitch shifting
	private float startShiftYCoord;
	bool shifting = false; //currently shifting a pitch
	float startShiftTime;
	float currShiftTime = 0;
	float totalTimeShifted = 0;//how much time has the player shifted for
	
	void Start ()
	{
		player = gameObject.GetComponent<PlayerMovement>();
		
	}
	
	void Update ()
	{
		if(targetItem && getDist (targetItem) > MAX_PICKUP_DIST) {
				targetItem = null;
		} 
		
		if (Input.GetKeyDown(KeyCode.Space)) {
				if(targetItem) {//if an item is targeted, grab it
					heldItem = targetItem;
					heldAudioSprite = heldItem.GetComponent<AudioSprite>();
					targetItem = null;
				}else if (heldItem) { //if no target, and an item is held, drop it
					heldItem = null;
					shifting = false;
				}
		}
		
		
		if(heldItem) { 
			heldItem.transform.position = new Vector3(transform.position.x, transform.position.y-.01f, transform.position.z);
			
			//start shifting
			if(Input.GetMouseButtonDown(0)) {
				shifting = true;
				startShiftYCoord = Input.mousePosition.y;
				heldAudioSprite.toggleBend();//start item's pitch bending
				startShiftTime = Time.timeSinceLevelLoad;//set start shift time
				currShiftTime = 0;
				
				
			} else if (Input.GetMouseButtonUp(0)) { //end shifting
				shifting = false;
				totalTimeShifted += currShiftTime;
				heldAudioSprite.toggleBend();//end item's pitch bending
			} 
			
			//player is shifting the item's pitch
			if(shifting && Input.GetMouseButton(0)) {
				handlePitchShift();
				updateShiftTime();
				updateJuju();
			}
		}
	}
	
	void updateShiftTime() {
		currShiftTime = Time.timeSinceLevelLoad-startShiftTime;
	}
	
	//update juju based on time shifted
	void updateJuju() {
		float sumShiftTime = totalTimeShifted + currShiftTime;
		player.Juju = 1 - (sumShiftTime/LIFETIME_SHIFT_TIME);
	}
	
	void handlePitchShift() {
		float currPos = Input.mousePosition.y;
		float yDiff = currPos - startShiftYCoord;
		float percentScreen = yDiff/Screen.height;
		heldAudioSprite.bendNote(percentScreen * AudioSprite.MAX_SHIFT);
	}
	
	float getDist(GameObject gO) {
		Vector2 myPos = new Vector2 (gameObject.transform.position.x, gameObject.transform.position.y);
		Vector2 otherPos = new Vector2(gO.transform.position.x, gO.transform.position.y);
		return (myPos-otherPos).magnitude;
	}
	
	
	
	//Handle collisions
	void OnControllerColliderHit (ControllerColliderHit hit)
	{
		//If I want to react to touching an npc
		/*if (hit.gameObject.CompareTag ("npc"))
			Debug.Log("hit npc");
		else */
		if (hit.gameObject.CompareTag ("item")){
			targetItem = hit.gameObject;
		}
	}
}



